<?php
class Third_Paginator extends Zend_Paginator{
	
	protected $cacheId;
	protected $cacheTags;
	
    public function _getCacheId( $page = null )
    {
        return $this->cacheId;
    }

	public function getCacheTags()
    {
        return $this->cacheTags;
    }
    
	public function setCacheTags( $cacheTags = array() )
    {
        $this->cacheTags = $cacheTags;
    }
    
    public function setCacheId( $cacheId, $cacheTags = array() )
    {
        $this->cacheId = $cacheId;
        $this->setCacheTags( $cacheTags );
    }

    
	/**
     * Factory.
     *
     * @param  mixed $data
     * @param  string $adapter
     * @param  array $prefixPaths
     * @return Zend_Paginator
     */
    public static function factory($data, $adapter = self::INTERNAL_ADAPTER,
                                   array $prefixPaths = null)
    {
        if ($adapter == self::INTERNAL_ADAPTER) {
            if (is_array($data)) {
                $adapter = 'Array';
            } else if ($data instanceof Zend_Db_Table_Select) {
                $adapter = 'DbTableSelect';
            } else if ($data instanceof Zend_Db_Select) {
                $adapter = 'DbSelect';
            } else if ($data instanceof Iterator) {
                $adapter = 'Iterator';
            } else if (is_integer($data)) {
                $adapter = 'Null';
            } else {
                $type = (is_object($data)) ? get_class($data) : gettype($data);

                /**
                 * @see Zend_Paginator_Exception
                 */
                require_once 'Zend/Paginator/Exception.php';

                throw new Zend_Paginator_Exception('No adapter for type ' . $type);
            }
        }

        $pluginLoader = self::getAdapterLoader();

        if (null !== $prefixPaths) {
            foreach ($prefixPaths as $prefix => $path) {
                $pluginLoader->addPrefixPath($prefix, $path);
            }
        }

        $adapterClassName = $pluginLoader->load($adapter);

        return new self(new $adapterClassName($data));
    }
    
   /**
     * Returns the items for a given page.
     *
     * @return Traversable
     */
    public function getItemsByPage($pageNumber)
    {
        $pageNumber = $this->normalizePageNumber($pageNumber);

        if ($this->_cacheEnabled()) {
            $data = self::$_cache->load( $this->_getCacheId($pageNumber) );
            if ($data !== false) {
                return $data;
            }
        }

        $offset = ($pageNumber - 1) * $this->_itemCountPerPage;

        $items = $this->_adapter->getItems($offset, $this->_itemCountPerPage);

        $filter = $this->getFilter();

        if ($filter !== null) {
            $items = $filter->filter($items);
        }

        if (!$items instanceof Traversable) {
            $items = new ArrayIterator($items);
        }

        if ($this->_cacheEnabled()) {
        	// Zend_Debug::dump( $this->getCacheTags() );
            self::$_cache->save($items, $this->_getCacheId($pageNumber), $this->getCacheTags() );
        }

        return $items;
    }
}

?>